package priv.pront.code.national.acwing.graph;

import java.util.Arrays;
import java.util.Scanner;

public class A858_Prim求最小生成树 {

    static int n, m;
    static int[][] g;
    static int[] dis;
    static boolean[] st;

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); m = scanner.nextInt();
        g = new int[n + 10][n + 10];
        dis = new int[n + 10];
        st = new boolean[n + 10];
        for (int i = 0; i < g.length; i++){Arrays.fill(g[i],(int)1e9);}
        for(int i = 0; i < m; i++){
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int z = scanner.nextInt();
            g[x][y] = g[y][x] = Math.min(g[x][y], z);
        }
        int t = prim();
        System.out.println(t == (int)1e9 ? "impossible" : t);
    }

    private static int prim(){
        Arrays.fill(dis, (int)1e9);
        int res = 0;
        for(int i = 0; i < n; i++){ // 更新n轮
            int t = -1;
            for(int j = 1; j <= n; j++){
                if(!st[j] && (t == -1 || dis[t] > dis[j])) t = j;
            }
            if(i != 0 && dis[t] == (int)1e9) return (int)1e9;
            if(i != 0) res += dis[t];
            for(int j = 1; j <= n; j++){
                dis[j] = Math.min(dis[j], g[t][j]);
            }
            st[t] = true;
        }
        return res;
    }
}
